<?php

class Database {
    private $conn;

    /**
     * 构造函数，接收 Database 类的连接
     * @param $database
     */
    public function __construct($database) {
        $this->conn = $database->connect();
    }

    /**
     * 插入数据
     * @param $table
     * @param $data
     * @return void
     */
    public function insert($table, $data) {
        $columns = implode(", ", array_keys($data));
        $placeholders = implode(", ", array_fill(0, count($data), '?'));
        $sql = "INSERT INTO $table ($columns) VALUES ($placeholders)";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            die("SQL 错误: " . $this->conn->error);
        }
        $types = str_repeat('s', count($data)); // 假设所有字段都是字符串类型
        $stmt->bind_param($types, ...array_values($data));
        return $stmt->execute();
    }

    /**
     * 更新数据
     * @param $table
     * @param $data
     * @param $condition
     * @return void
     */
    public function update($table, $data, $condition) {
        $set = [];
        foreach ($data as $key => $value) {
            $set[] = "$key = ?";
        }
        $set = implode(", ", $set);
        $sql = "UPDATE $table SET $set WHERE $condition";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            die("SQL 错误: " . $this->conn->error);
        }
        $types = str_repeat('s', count($data)); // 假设所有字段都是字符串类型
        $stmt->bind_param($types, ...array_values($data));
        return $stmt->execute();
    }

    /**
     * 删除数据
     * @param $table
     * @param $condition
     * @return void
     */
    public function delete($table, $condition) {
        $sql = "DELETE FROM $table WHERE $condition";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            die("SQL 错误: " . $this->conn->error);
        }
        return $stmt->execute();
    }

    /**
     * 查询单条数据
     * @param $table
     * @param $condition
     * @param $columns
     * @return void
     */
    public function get($table, $condition, $columns = '*') {
        $sql = "SELECT $columns FROM $table WHERE $condition";
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            die("SQL 错误: " . $this->conn->error);
        }
        $stmt->execute();
        $result = $stmt->get_result();
        return $result->fetch_assoc();
    }

    /**
     * 查询多条数据（获取列表）
     * @param $table
     * @param $condition
     * @param $columns
     * @param $limit
     * @param $order
     * @return void
     */
    public function getAll($table, $condition = '1', $columns = '*', $limit = null, $order = null) {
        $sql = "SELECT $columns FROM $table WHERE $condition";
        if ($order) {
            $sql .= " ORDER BY $order";
        }
        if ($limit) {
            $sql .= " LIMIT $limit";
        }
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            die("SQL 错误: " . $this->conn->error);
        }
        $stmt->execute();
        $result = $stmt->get_result();
        return $result->fetch_all(MYSQLI_ASSOC);
    }

    /**
     * 获取最后插入的ID
     * @return mixed
     */
    public function getLastInsertId() {
        return $this->conn->insert_id;
    }

    /**
     * 关闭连接
     * @return void
     */
    public function close() {
        $this->conn->close();
    }
}